/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin.files;

import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Iterator;
import java.util.Random;

/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 26, 2009
 */
public class FileUtils {
    public static void copyFile(File in, File out) throws IOException {
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        }
        catch (IOException e) {
            throw e;
        }
        finally {
            if (inChannel != null) inChannel.close();
            if (outChannel != null) outChannel.close();
        }
    }

    public static void copyTwoFileIntoOne(File in1, File in2, File out) throws IOException {
        FileChannel inChannel1 = new FileInputStream(in1).getChannel();
        FileChannel inChanne12 = new FileInputStream(in2).getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream(out);
        FileChannel outChannel = fileOutputStream.getChannel();
        try {
            inChannel1.transferTo(0, inChannel1.size(), outChannel);
            fileOutputStream.write(13); // end of line
            inChanne12.transferTo(0, inChanne12.size(), outChannel);
        }
        catch (IOException e) {
            throw e;
        }
        finally {
            if (inChannel1 != null) inChannel1.close();
            if (inChanne12 != null) inChanne12.close();
            if (outChannel != null) outChannel.close();
        }
    }

    public static void copyFilesIntoOne(File out, Iterator<File> in) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(out);
        FileChannel outChannel = fileOutputStream.getChannel();
        try {
            while (in.hasNext()) {
                FileChannel inChanne1 = null;
                try {
                    File inFile = in.next();
                    inChanne1 = new FileInputStream(inFile).getChannel();
                    inChanne1.transferTo(0, inChanne1.size(), outChannel);
                    fileOutputStream.write(13); // end of line
                } finally {
                    if (inChanne1 != null) inChanne1.close();
                }
            }
        }
        catch (IOException e) {
            throw e;
        }
        finally {
            if (outChannel != null) outChannel.close();
        }
    }

    public static File createaTmpDir() {
        String tmpPath = System.getProperty("java.io.tmpdir");
        File tmpDir = new File(tmpPath, System.currentTimeMillis() + new Random().nextInt() + "");
        if (!tmpDir.mkdir())
            throw new IllegalStateException("Tmp Directory [" + tmpDir.getAbsolutePath() + "] has not been created!");

        return tmpDir;
    }

    public static boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteDirectory(file);
                } else {
                    file.delete();
                }
            }
        }
        return path.delete();
    }

    public static String readFileContent(File file) throws IOException {
        return new JsCssMinFileReader(file).read();
    }

    public static String readFileContent(File file, String encoding) throws IOException {
        return new JsCssMinFileReaderEncoding(file, encoding).read();
    }

    public static void writeContentIntoFile(File file, String content) throws IOException {
        FileWriter writer = null;
        try {
            writer = new FileWriter(file);
            writer.write(content);
        } finally {
            if (writer != null)
                writer.close();

        }
    }
}
